/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sains.aging.util;

import java.sql.Connection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.jooq.ConnectionProvider;
import org.jooq.exception.DataAccessException;

/**
 *
 * @author fikri
 */
public class DBUtil implements ConnectionProvider{
    
    
    private static DataSource dataSource = null;
    
    private static DBUtil instance =
                new DBUtil();

    public static DBUtil getInstance()   {
        return instance;
    }   
    
    @Override
    public Connection acquire() throws DataAccessException {
        Connection conn = null;
        try{
            //System.out.println("DSN is " + DS);
            if (dataSource == null) {
                    Properties prop1 = new Properties();
                    Properties prop2 = new Properties();
                    Properties prop3 = new Properties();
                    prop1.setProperty("date format","iso");
                    prop2.setProperty("time format","iso");
                    prop3.setProperty("setTransactionIsolation","SERIALIZABLE");
                    Context initContext = new InitialContext();
                    initContext.addToEnvironment("date format",prop1);
                    initContext.addToEnvironment("time format",prop2);
                    initContext.addToEnvironment("setTransactionIsolation", prop3);
                    dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/sainsDB");
                }            
            
                conn = dataSource.getConnection();
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }

    @Override
    public void release(Connection conn) throws DataAccessException {
        
        try {
            conn.close();
        } catch (Exception ex) {
        }
        
    }
    
}
